package com.platform.modules.wallet.take;

import cn.hutool.core.date.DateUtil;
import com.platform.common.config.PlatformConfig;
import com.platform.common.enums.YesOrNoEnum;
import com.platform.modules.chat.dao.ChatMsgDao;
import com.platform.modules.chat.domain.ChatGroupMonitor;
import com.platform.modules.chat.service.ChatGroupMonitorService;
import com.platform.modules.chat.service.ChatMsgService;
import com.platform.modules.wallet.service.WalletAccountService;
import com.platform.modules.wallet.service.WalletRedPacketService;
import com.platform.modules.wallet.service.WalletRedPacketSubService;
import com.platform.modules.wallet.service.WalletTransferService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class ClearTask {

    @Resource
    private WalletTransferService transferService;
    @Resource
    private WalletRedPacketService redPacketService;
    @Resource
    private WalletRedPacketSubService redPacketSubService;
    @Resource
    private WalletAccountService accountService;
    @Resource
    private ChatMsgService msgService;
    @Resource
    private ChatGroupMonitorService groupMonitorService;

    @Scheduled(cron = "0 0 2 * * ?")//"0 5/10 9-23 * * ?"
    public void runClearRecord() {

        if (PlatformConfig.MASTER.equals(YesOrNoEnum.NO)) return;

        String yesterday = DateUtil.formatDate(DateUtil.yesterday());
        msgService.clearRecord(yesterday);
        transferService.clearRecord(yesterday);
        redPacketService.clearRecord(yesterday);
        redPacketSubService.clearRecord(yesterday);
        accountService.clearRecord(yesterday);
    }

    @Scheduled(cron = "30 0 0 * * ?")//"0 5/10 9-23 * * ?"
    public void clearMonitorData() {
        List<ChatGroupMonitor> chatGroupMonitors = groupMonitorService.queryList(new ChatGroupMonitor());
        for (ChatGroupMonitor chatGroupMonitor : chatGroupMonitors) {
            chatGroupMonitor.setInTotal(0);
            chatGroupMonitor.setInBalance(0);
            chatGroupMonitor.setOutTotal(0);
            chatGroupMonitor.setOutBalance(0);
            chatGroupMonitor.setUpdateTime(DateUtil.date());
            chatGroupMonitor.updateById();
        }
    }
}
